
public class Liczba {

	private int number;
	
	public Liczba(){
		number = 0;
	}
	
	public Liczba(int number){
		this.number = number;
	}
	
	public Liczba(String number, int base) throws LiczbaException{
		if(base < 2 || base > 16) throw new LiczbaException("Zla podstawa!");
		int length = number.length();
		char litera[] = {'A', 'B', 'C', 'D', 'E', 'F'};
		byte cyfra[] = { 10, 11, 12, 13, 14, 15 };
		int result = 0;
		
		for(int i = length - 1; i >= 0; --i){
			int tmp1 = 0, tmp2 = 0;
			char k = number.charAt(i);
			
			for(int j = 0; j < 6; ++j){
				if(k == litera[j]){
					if(base <= cyfra[j]) throw new LiczbaException("Zle dane!");
					tmp1 = cyfra[j];
					++tmp2;
					break;
				}
			}
			
			if(tmp2 == 0)
				try{tmp1 = Integer.parseInt( number.substring(i, i + 1)); }
				catch(NumberFormatException e){
					throw new LiczbaException("Zla dana!");
				}
			if(tmp1 >= base) throw new LiczbaException("Zla dana!");
			result += (int)(Math.pow(base, (length - i - 1))) * tmp1;			
		}
		this.number = result;
	}
	
	public String zapis(int base) throws LiczbaException{
		int n = number;
		if(base < 2 || base > 16) throw new LiczbaException("Zla podstawa!");
		char litera[] = {'A', 'B', 'C', 'D', 'E', 'F'};
		byte cyfra[] = { 10, 11, 12, 13, 14, 15 };
		
		String w1 = "", w2 = "";
		while(n > 0){
			int tmp = n % base;
			if(tmp >= 10)
				for(int i = 0; i < 6; ++i){
					if(tmp == cyfra[i])
						w1 += litera[i];
				}
			else w1 += Integer.toString(tmp);
			n = n/base;
		}
		for(int i = w1.length() - 1; i >= 0; --i)
			w2 += w1.charAt(i);
		
		return w2;
	}
	
	public String zapis10(){
		return Integer.toString(number);
	}
	
}
